
%macro junker(er);
data Ins_23_reprod_inp_counts;
   merge inter.Ins_23_reprod_AZ inter.ins_23_reprod_ca_preg inter.Ins_23_reprod_IA 
         inter.Ins_23_reprod_NY inter.Ins_23_reprod_TX inter.Ins_23_reprod_WI;
   by months_23;
  %macro m_array(st);
     array &ST._&group._{5} &ST._&group. &ST._medi_&group. &ST._priv_&group. &ST._self_&group. &ST._o_ins_&group.; 
  %mend;
  %macro comb(er);
      TOT_&group._{1} = sum(AZ_&group._{1},CA_&group._{1},IA_&group._{1},NY_&group._{1},TX_&group._{1},WI_&group._{1},0);
      do i = 2 to 5;
          TOT_&group._{i} = sum((AZ_&group._{1}/TOT_&group._{1})*AZ_&group._{i},(CA_&group._{1}/TOT_&group._{1})*CA_&group._{i},(IA_&group._{1}/TOT_&group._{1})*IA_&group._{i},(NY_&group._{1}/TOT_&group._{1})*NY_&group._{i},(TX_&group._{1}/TOT_&group._{1})*TX_&group._{i},(WI_&group._{1}/TOT_&group._{1})*WI_&group._{i},0);
      end;
   %mend;
  %macro Group(group);
     %m_array(AZ);
     %m_array(CA);
     %m_array(IA);
     %m_array(NY);
     %m_array(TX);
     %m_array(WI);
     %m_array(TOT);
	 %comb(er);
  %mend;
  %group(ALL);
  %group(ALL_ED);
  %group(ALL_NED);
  %group(ALL_NP);
  %group(ALL_NP_ED);
  %group(ALL_NP_NED);  
  %group(MALE); 
  %group(MALE_ED);  
  %group(MALE_NED);   
  %group(FEMALE);  
  %group(FEMALE_ED);  
  %group(FEMALE_NED);  
  %group(FEMALE_NP);  
  %group(FEMALE_NP_ED);  
  %group(FEMALE_NP_NED);  
  %group(FEMALE_P);  
  %group(FEMALE_P_ED);  
  %group(FEMALE_P_NED);  
  %group(ALL_NP_PUB);  
  %group(ALL_NP_PUB_ED);  
  %group(ALL_NP_PUB_NED);  
  %group(ALL_NP_NON_PROF);  
  %group(ALL_NP_NON_PROF_ED);  
  %group(ALL_NP_NON_PROF_NED);  
  %group(ALL_NP_FOR_PROF);  
  %group(ALL_NP_FOR_PROF_ED);  
  %group(ALL_NP_FOR_PROF_NED);  
  merge_v = 1;
run;  
%mend;
%junker(er);

data inter.Ins_23_reprod_inp_rates;
   merge Ins_23_reprod_inp_counts inter.ins_23_pop_combined_inp;
   by merge_v;
   age = 23+months_23/12;
   if months_23 = 0 then bmonth = 1; else bmonth = 0;
   if months_23 >0 then post = 1; else post = 0;
   linear = months_23;
   linear_post = months_23*post;
   array lev_all{15} TOT_ALL   TOT_ALL_ED   TOT_ALL_NED   TOT_ALL_NP   TOT_ALL_NP_ED   TOT_ALL_NP_NED   TOT_ALL_NP_PUB   TOT_ALL_NP_PUB_ED   TOT_ALL_NP_PUB_NED   TOT_ALL_NP_NON_PROF   TOT_ALL_NP_NON_PROF_ED   TOT_ALL_NP_NON_PROF_NED   TOT_ALL_NP_FOR_PROF   TOT_ALL_NP_FOR_PROF_ED   TOT_ALL_NP_FOR_PROF_NED;
   array log_all{15} TOT_ALL_l TOT_ALL_ED_l TOT_ALL_NED_l TOT_ALL_NP_l TOT_ALL_NP_ED_l TOT_ALL_NP_NED_l TOT_ALL_NP_PUB_l TOT_ALL_NP_PUB_ED_l TOT_ALL_NP_PUB_NED_l TOT_ALL_NP_NON_PROF_l TOT_ALL_NP_NON_PROF_ED_l TOT_ALL_NP_NON_PROF_NED_l TOT_ALL_NP_FOR_PROF_l TOT_ALL_NP_FOR_PROF_ED_l TOT_ALL_NP_FOR_PROF_NED_l;
   array ra_all{15}  TOT_ALL_r TOT_ALL_ED_r TOT_ALL_NED_r TOT_ALL_NP_r TOT_ALL_NP_ED_r TOT_ALL_NP_NED_r TOT_ALL_NP_PUB_r TOT_ALL_NP_PUB_ED_r TOT_ALL_NP_PUB_NED_r TOT_ALL_NP_NON_PROF_r TOT_ALL_NP_NON_PROF_ED_r TOT_ALL_NP_NON_PROF_NED_r TOT_ALL_NP_FOR_PROF_r TOT_ALL_NP_FOR_PROF_ED_r TOT_ALL_NP_FOR_PROF_NED_r;
   array lev_male{3} TOT_MALE   TOT_MALE_ED   TOT_MALE_NED;
   array log_male{3} TOT_MALE_l TOT_MALE_ED_l TOT_MALE_NED_l;
   array ra_male{3}  TOT_MALE_r TOT_MALE_ED_r TOT_MALE_NED_r;
   array lev_fem{9} TOT_FEMALE   TOT_FEMALE_ED   TOT_FEMALE_NED   TOT_FEMALE_NP   TOT_FEMALE_NP_ED   TOT_FEMALE_NP_NED 		TOT_FEMALE_P   TOT_FEMALE_P_ED   TOT_FEMALE_P_NED; 
   array log_fem{9} TOT_FEMALE_l TOT_FEMALE_ED_l TOT_FEMALE_NED_l TOT_FEMALE_NP_l TOT_FEMALE_NP_ED_l TOT_FEMALE_NP_NED_l 	TOT_FEMALE_P_l TOT_FEMALE_P_ED_l TOT_FEMALE_P_NED_l; 
   array ra_fem{9}  TOT_FEMALE_r TOT_FEMALE_ED_r TOT_FEMALE_NED_r TOT_FEMALE_NP_r TOT_FEMALE_NP_ED_r TOT_FEMALE_NP_NED_r	TOT_FEMALE_P_r TOT_FEMALE_P_ED_r TOT_FEMALE_P_NED_r; 
   do i = 1 to 15;
      log_all{i}= log(lev_all{i});
	  ra_all{i}= 10000*lev_all{i}/pop_all;
   end;
   do i = 1 to 3;
      log_male{i}= log(lev_male{i});
	  ra_male{i}= 10000*lev_male{i}/pop_male;
   end;
   do i = 1 to 9;
      log_fem{i}= log(lev_fem{i});
	  ra_fem{i}= 10000*lev_fem{i}/pop_female;
   end;
   AZ_ALL_l = log(AZ_ALL);
   CA_ALL_l = log(CA_ALL);
   IA_ALL_l = log(IA_ALL);
   NY_ALL_l = log(NY_ALL);
   TX_ALL_l = log(TX_ALL);
   WI_ALL_l = log(WI_ALL);
run;
  
PROC EXPORT DATA= inter.Ins_23_reprod_inp_rates
            OUTFILE= "C:\Research\Insurance 23\3. Code up from Scratch\P10 Inpatient Analysis File.csv"
            DBMS=CSV REPLACE;
RUN;

proc means data = Ins_23_reprod_inp_counts sum;
   var TOT_ALL_NP  TOT_ALL_NP_ED   TOT_ALL_NP_NED  TOT_ALL_NP_PUB         TOT_ALL_NP_NON_PROF         TOT_ALL_NP_FOR_PROF;
   where   months_23 >= -12 and months_23 <= 11;
   title "Counts for tables";
run;

%let keep_v = TOT_ALL 				TOT_medi_ALL 				TOT_priv_ALL 				TOT_self_ALL 				TOT_o_ins_ALL
			  TOT_ALL_ED 			TOT_medi_ALL_ED 			TOT_priv_ALL_ED 			TOT_self_ALL_ED 			TOT_o_ins_ALL_ED
			  TOT_ALL_NED 			TOT_medi_ALL_NED 			TOT_priv_ALL_NED 			TOT_self_ALL_NED 			TOT_o_ins_ALL_NED
			  TOT_ALL_NP 			TOT_medi_ALL_NP 			TOT_priv_ALL_NP 			TOT_self_ALL_NP 			TOT_o_ins_ALL_NP
			  TOT_ALL_NP_ED 		TOT_medi_ALL_NP_ED 			TOT_priv_ALL_NP_ED 			TOT_self_ALL_NP_ED 			TOT_o_ins_ALL_NP_ED
			  TOT_ALL_NP_NED 		TOT_medi_ALL_NP_NED 		TOT_priv_ALL_NP_NED 		TOT_self_ALL_NP_NED 		TOT_o_ins_ALL_NP_NED
			  TOT_MALE 				TOT_medi_MALE 				TOT_priv_MALE 				TOT_self_MALE 				TOT_o_ins_MALE
			  TOT_MALE_ED 			TOT_medi_MALE_ED 			TOT_priv_MALE_ED 			TOT_self_MALE_ED 			TOT_o_ins_MALE_ED
			  TOT_MALE_NED 			TOT_medi_MALE_NED 			TOT_priv_MALE_NED 			TOT_self_MALE_NED 			TOT_o_ins_MALE_NED
			  TOT_FEMALE 			TOT_medi_FEMALE 			TOT_priv_FEMALE 			TOT_self_FEMALE 			TOT_o_ins_FEMALE
			  TOT_FEMALE_ED 		TOT_medi_FEMALE_ED 			TOT_priv_FEMALE_ED 			TOT_self_FEMALE_ED 			TOT_o_ins_FEMALE_ED
			  TOT_FEMALE_NED 		TOT_medi_FEMALE_NED 		TOT_priv_FEMALE_NED 		TOT_self_FEMALE_NED 		TOT_o_ins_FEMALE_NED
			  TOT_FEMALE_NP 		TOT_medi_FEMALE_NP 			TOT_priv_FEMALE_NP 			TOT_self_FEMALE_NP 			TOT_o_ins_FEMALE_NP
			  TOT_FEMALE_NP_ED 		TOT_medi_FEMALE_NP_ED 		TOT_priv_FEMALE_NP_ED 		TOT_self_FEMALE_NP_ED 		TOT_o_ins_FEMALE_NP_ED
			  TOT_FEMALE_NP_NED 	TOT_medi_FEMALE_NP_NED 		TOT_priv_FEMALE_NP_NED 		TOT_self_FEMALE_NP_NED 		TOT_o_ins_FEMALE_NP_NED
			  TOT_FEMALE_P 			TOT_medi_FEMALE_P 			TOT_priv_FEMALE_P 			TOT_self_FEMALE_P 			TOT_o_ins_FEMALE_P
			  TOT_FEMALE_P_ED 		TOT_medi_FEMALE_P_ED 		TOT_priv_FEMALE_P_ED 		TOT_self_FEMALE_P_ED 		TOT_o_ins_FEMALE_P_ED
			  TOT_FEMALE_P_NED 		TOT_medi_FEMALE_P_NED 		TOT_priv_FEMALE_P_NED 		TOT_self_FEMALE_P_NED 		TOT_o_ins_FEMALE_P_NED
			  TOT_ALL_NP_PUB 			TOT_medi_ALL_NP_PUB 			TOT_priv_ALL_NP_PUB 			TOT_self_ALL_NP_PUB 			TOT_o_ins_ALL_NP_PUB
			  TOT_ALL_NP_PUB_ED 		TOT_medi_ALL_NP_PUB_ED 		    TOT_priv_ALL_NP_PUB_ED 			TOT_self_ALL_NP_PUB_ED 			TOT_o_ins_ALL_NP_PUB_ED
			  TOT_ALL_NP_PUB_NED 		TOT_medi_ALL_NP_PUB_NED 		TOT_priv_ALL_NP_PUB_NED 		TOT_self_ALL_NP_PUB_NED 		TOT_o_ins_ALL_NP_PUB_NED
			  TOT_ALL_NP_NON_PROF 		TOT_medi_ALL_NP_NON_PROF 		TOT_priv_ALL_NP_NON_PROF 		TOT_self_ALL_NP_NON_PROF 		TOT_o_ins_ALL_NP_NON_PROF
			  TOT_ALL_NP_NON_PROF_ED 	TOT_medi_ALL_NP_NON_PROF_ED 	TOT_priv_ALL_NP_NON_PROF_ED 	TOT_self_ALL_NP_NON_PROF_ED 	TOT_o_ins_ALL_NP_NON_PROF_ED
			  TOT_ALL_NP_NON_PROF_NED 	TOT_medi_ALL_NP_NON_PROF_NED 	TOT_priv_ALL_NP_NON_PROF_NED 	TOT_self_ALL_NP_NON_PROF_NED 	TOT_o_ins_ALL_NP_NON_PROF_NED
			  TOT_ALL_NP_FOR_PROF 		TOT_medi_ALL_NP_FOR_PROF 		TOT_priv_ALL_NP_FOR_PROF 		TOT_self_ALL_NP_FOR_PROF 		TOT_o_ins_ALL_NP_FOR_PROF
			  TOT_ALL_NP_FOR_PROF_ED 	TOT_medi_ALL_NP_FOR_PROF_ED 	TOT_priv_ALL_NP_FOR_PROF_ED 	TOT_self_ALL_NP_FOR_PROF_ED 	TOT_o_ins_ALL_NP_FOR_PROF_ED
			  TOT_ALL_NP_FOR_PROF_NED 	TOT_medi_ALL_NP_FOR_PROF_NED 	TOT_priv_ALL_NP_FOR_PROF_NED 	TOT_self_ALL_NP_FOR_PROF_NED 	TOT_o_ins_ALL_NP_FOR_PROF_NED
  			  TOT_ALL_l TOT_ALL_ED_l TOT_ALL_NED_l TOT_ALL_NP_l TOT_ALL_NP_ED_l TOT_ALL_NP_NED_l TOT_ALL_NP_PUB_l TOT_ALL_NP_PUB_ED_l TOT_ALL_NP_PUB_NED_l TOT_ALL_NP_NON_PROF_l TOT_ALL_NP_NON_PROF_ED_l TOT_ALL_NP_NON_PROF_NED_l TOT_ALL_NP_FOR_PROF_l TOT_ALL_NP_FOR_PROF_ED_l TOT_ALL_NP_FOR_PROF_NED_l
 			  TOT_ALL_r TOT_ALL_ED_r TOT_ALL_NED_r TOT_ALL_NP_r TOT_ALL_NP_ED_r TOT_ALL_NP_NED_r TOT_ALL_NP_PUB_r TOT_ALL_NP_PUB_ED_r TOT_ALL_NP_PUB_NED_r TOT_ALL_NP_NON_PROF_r TOT_ALL_NP_NON_PROF_ED_r TOT_ALL_NP_NON_PROF_NED_r TOT_ALL_NP_FOR_PROF_r TOT_ALL_NP_FOR_PROF_ED_r TOT_ALL_NP_FOR_PROF_NED_r

	          TOT_FEMALE   TOT_FEMALE_ED   TOT_FEMALE_NED   TOT_FEMALE_NP   TOT_FEMALE_NP_ED   TOT_FEMALE_NP_NED 		TOT_FEMALE_P   TOT_FEMALE_P_ED   TOT_FEMALE_P_NED
              TOT_FEMALE_l TOT_FEMALE_ED_l TOT_FEMALE_NED_l TOT_FEMALE_NP_l TOT_FEMALE_NP_ED_l TOT_FEMALE_NP_NED_l 	TOT_FEMALE_P_l TOT_FEMALE_P_ED_l TOT_FEMALE_P_NED_l 
              TOT_FEMALE_r TOT_FEMALE_ED_r TOT_FEMALE_NED_r TOT_FEMALE_NP_r TOT_FEMALE_NP_ED_r TOT_FEMALE_NP_NED_r	TOT_FEMALE_P_r TOT_FEMALE_P_ED_r TOT_FEMALE_P_NED_r
;

data inter.mike_tal;
   set inter.Ins_23_reprod_inp_rates (keep = months_23 &keep_v.    linear   post  linear_post );
   if months_23 >= -36 and months_23 <= 36;
run;

PROC EXPORT DATA= inter.mike_tal
            OUTFILE= "C:\Research\Insurance 23\3. Code up from Scratch\P10 Inpatient CSV File.csv"
            DBMS=CSV REPLACE;
RUN;
